{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <style>
        .title-list {
            min-height: 100px;
            border-right: 1px solid #dddddd;
        }

        .content {
            min-height: 500px;
            border-left: 1px solid #dddddd;
            margin-left: -1px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock %}
{% block cotent %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading clearfix"><i class="fa fa-book" aria-hidden="true"></i> wiki知识库
                {% if wiki_obj %}
                    <span><a style="float: right;margin-left: 10px" type="button" class="btn btn-sm btn-danger"
                             href="{% url 'wiki_delete' project_id=request.manage.id wiki_id=wiki_obj.id %}">删 除</a></span>
                    <span><a style="float: right;margin-left: 10px" type="button" class="btn btn-sm btn-primary"
                             href="{% url 'wiki_edit' project_id=request.manage.id wiki_id=wiki_obj.id %}">编 辑</a></span>
                {% endif %}
                <span><a style="float: right" type="button" class="btn btn-sm btn-success"
                         href="{% url 'wiki_add' project_id=request.manage.id %}">添 加</a></span>
            </div>
            <div class="panel-body">
                <div class="col-xs-3 title-list">
                    <ul id="catalog">

                    </ul>
                </div>
                <div class="col-xs-9 content">
                    {% if wiki_obj %}
                        <div id="mdeitor">
                        <textarea>{{ wiki_obj.cotent }}</textarea>
                        </div>
                        {% else %}
                        <div style="text-align: center;margin-top: 80px">
                            <h4>《{{ request.manage.name }}》wiki文档</h4>
                            <a href="{% url 'wiki_add' project_id=request.manage.id %}"><span
                                    class="glyphicon glyphicon-plus" aria-hidden="true"></span>添加文章</a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
    <script src="{% static 'plugin/editor.md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md/lib/jquery.flowchart.min.js' %}"></script>
    <script>
        $(function () {
            init_cata();
            init_editor();
        })
        function init_editor() {
            editormd.markdownToHTML('mdeitor',{
                htmlDecode:"style,script,iframe"
            });
        }
        function init_cata() {
            var wiki_url = '{% url 'wiki' project_id=request.manage.id %}';
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.manage.id %}',
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            // item=["id":1,"title":目录1，"parent_id":null]
                            var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").text(item.title).attr("href", wiki_url + "?wiki_id=" + item.id)).append($("<ul>"));
                            if (!item.parent_id) {
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li);
                            }
                        })
                    } else {
                        alert("初始化目录失败");
                    }
                }
            })
        }
    </script>
{% endblock %}